﻿@model thailandProject.Models.Pair<List<thailandProject.Models.TASK>, List<thailandProject.Models.TASKTYPE>>

@{
    ViewBag.Title = "Dashboard - Welcome!";
}
<!-- Div wrapper2 starts here -->
<div id="Wrapper2">
    <div id="content_header">
        <div id="headerimage">
        </div>
        <div id="theheader">@ViewBag.Header</div>
    </div>
    <!-- Sidebar Element Starts Here -->
    <aside id="sidebar-wrapper">
        <nav class="sidebar">
        <h1 id="lblUpcomingTasks">Upcoming Tasks</h1>
        <div style="width:100%; float:left; margin:10px 0px 0px 0px;">
            <div id="taskfilters">
                <span style="float:left; vertical-align:middle; margin:2px 2px 10px 2px;">
                    @if(Session["ContextClient"] != null)
                    {
                        thailandProject.Models.CLIENT client = (thailandProject.Models.CLIENT) Session["ContextClient"];
                        string name = client.clientFirstName + " " + (String.IsNullOrEmpty(client.clientLastName) ? "" : client.clientLastName.Substring(0, 1) + ".");
                        @Html.ActionLink(name, "Index", new { sortType = client.clientID + "" }, new { style="font-size:1.2em", @class="home_links" }) @:|
                    }
                    @Html.ActionLink("Show All", "Index", new { sortType = "" }, new { style="font-size:1.2em", @class="home_links" }) |
                    @for (int i = 0; i < Model.value2.Count - 1; i++ )
                    {
                        var taskType = Model.value2[i];
                        @Html.ActionLink(taskType.taskTypeName, "Index", new { sortType = taskType.taskTypeName }, new { style = "font-size:1.2em", @class = "home_links" }) @:|
                    }
                    @Html.ActionLink(Model.value2.Last().taskTypeName, "Index", new { sortType = Model.value2.Last().taskTypeName }, new { style = "font-size:1.2em", @class = "home_links" })
                </span>
            </div>
            <div id="taskothers">
                <div style="width:100%; float:left;">
                    @using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "taskOptions" }))
                    {
                        string prevValue = HttpContext.Current.Session["taskDays"].ToString();
                        string value = "true";
                        
                        <!-- In the future, we'll want to change this because there's a HTML.DropDownList that makes this so much easier -->
                        <div style="width:100%; float:left;">
                        <div style="float:left;">
                        <select id="daysfromnow" name="days" onchange="this.form.submit()">
                            <option value="1" @if (prevValue == "1") { @:selected="selected"
                                 } >1</option>
                            <option value="2" @if (prevValue == "2") { @:selected="selected"
                                 }>2</option>
                            <option value="3" @if (prevValue == "3") { @:selected="selected"
                                 }>3</option>
                            <option value="4" @if (prevValue == "4") { @:selected="selected"
                                 }>4</option>
                            <option value="5" @if (prevValue == "5") { @:selected="selected"
                                 }>5</option>
                            <option value="7" @if (prevValue == "7") { @:selected="selected"
                                 }>7</option>
                            <option value="14" @if (prevValue == "14") { @:selected="selected"
                                 }>14</option>
                            <option value="21" @if (prevValue == "21") { @:selected="selected"
                                 }>21</option>
                            <option value="28" @if (prevValue == "28") { @:selected="selected"
                                 }>28</option>
                        </select>
                        </div>
                        <div style="float:left; margin:4px;">days from now</div>
                        </div>
                        <br />
                        <span>Hide completed </span>
                        if (Session["hideComplete"] != null)
                        {
                            value = (bool)Session["hideComplete"] ? "true" : "false";
                        }
                        <input type="hidden" id="precheckHide" value="@value" />
                        <span>
                            <input class="hideCompleted" id="hideCompleted" name="hideCompleted" type="hidden" value="true" />
                            <img class="hideCompleted_checkbox" src="../../images/checkedBlue.png" id="hideCompleted_checkbox" style="width:1.4em;"/>
                            <img src="../../images/unchecked.png" id="image_preload" style="width:1.4em; visibility:hidden;"/>   
                        </span>
                        <input type="hidden" name="hideComplete" value="false" /> <!-- This will be used when the checkbox is not checked -->
                        <!--<input type="text" name="days" value="@HttpContext.Current.Session["taskDays"].ToString()" maxlength="2" style="width:1.5em; height:.75em"/>
                        <input type="submit" value="Set" style="height:2.1em; font-size:1.2em" />-->
                    }
                </div>
            </div>
        </div>
            <div>
                
                <span style="float:right">
                    
                </span>
                <br />
            </div>
            <br />
            

            @if (Model.value1.Count == 0)
            {
                <p>No upcoming tasks for the next @HttpContext.Current.Session["taskDays"].ToString() days</p>
            }
            @foreach (var tc in Model.value1)
            {
                string completed = tc.taskComplete.ToString();
                string date = tc.taskDueDate.Month + "/" + tc.taskDueDate.Day + "/" + tc.taskDueDate.Year + " ";
                bool AM = true;
                string clientName = tc.RESERVATION.CLIENT.clientFirstName + " " + (String.IsNullOrEmpty(tc.RESERVATION.CLIENT.clientLastName) ?
                    "" : tc.RESERVATION.CLIENT.clientLastName.Substring(0, 1) + ".");

                if (tc.taskDueDate.Hour == 0)
                {
                    date += "12";
                }
                else if (tc.taskDueDate.Hour < 12)
                {
                    date += tc.taskDueDate.Hour;
                }
                else if (tc.taskDueDate.Hour == 12)
                {
                    date += tc.taskDueDate.Hour;
                    AM = false;
                }
                else
                { //It's 1 PM to 11 PM
                    date += tc.taskDueDate.Hour - 12;
                    AM = false;
                }

                date += ":";
                if (tc.taskDueDate.Minute < 10)
                {
                    date += "0" + tc.taskDueDate.Minute;
                }
                else
                {
                    date += tc.taskDueDate.Minute;
                }

                if (AM)
                {
                    date += " AM";
                }
                else
                {
                    date += " PM";
                }
                
                <div class="pickups_header" @if(tc.taskTypeID == 0) {
                                                @:style="background: url('/images/menubar_gold.jpg') repeat-x scroll bottom left transparent;"
                                                }>
                    <div class="pickup_checkbox">
                        <!--<input id="pickUpCheckbox" type="checkbox"/>-->
                        <input class="checkcompleted"  type="hidden" value="@completed" />
                        <img class="this_check_box" src="../../images/unchecked.png" id="@Html.DisplayFor(modelItem => tc.taskID)"/>  
                    </div>
                    <div class="pickups_subheader" onclick="$(this).parent().next().slideToggle('fast');">
                        <div class="guestname">
                            @clientName
                        </div>
                        <div class="datetime">
                            @date
                        </div>
                        <div class="location">
                            @Html.DisplayFor(modelItem => tc.taskName)
                        </div>
                    </div>
                </div>
                
                
                <!--Dropdown-->
                <div class="pickups_body" style="display:none">
                    <div class="subitems_title">Apartment @Html.DisplayFor(modelItem => tc.RESERVATION.PROPERTY.propName)</div>
                    <div class="space_10"></div>
                    <div class="subitems_body">@Html.DisplayFor(modelItem => tc.taskDetails)</div>
                    <div class="space_10"></div>
                    @if((int)Session["userPerm"] <= 2)
                    {
                        string edit_link = "/Home/EditTask?taskId=" + tc.taskID + "&redirectURL=" + Request.Url.AbsoluteUri;
                        string delete_link = "/Home/DeleteTaskHome?taskId=" + tc.taskID + "&redirectURL=" + Request.Url.AbsoluteUri;
                        <div class="edit-delete" style="float:right;">
                            <a href="@delete_link" onclick="return confirm('Are you sure?')"><img src="../../images/delete_button_small.png" alt="delete" /></a>
                            <a href="@edit_link"><img src="../../images/edit_icon_small.png" alt="edit" /></a>
                        </div>
                    }
                </div>
            }   
        </nav>
        
        <!-- Sidebar Element Ends Here -->
    </aside>
    <!-- Another Sidebar Element Ends Here -->
    <!-- Main view container starts -->
    <article id="contents">
        This is the dashboard. All upcoming tasks from now to a specified amount of days are displayed.
        <br /><br />
        Click on a sorting link to only show tasks of that type. Clicking on "Hide Completed" will hide all tasks
        that have been marked as completed.
</article>
    <!-- Main view container ends -->
</div>
<!-- Div wrapper2 ends here -->

<script>
    $("document").ready(function () {
        $(function () {
            if ($("#precheckHide").val() == "false") {
                $("#hideCompleted_checkbox").attr('src', '../../images/unchecked.png');
            }
        })
        
        $(".hideCompleted_checkbox").click(function () {
            var indicator = 0;
            var this_id = $(this).attr('id');
            //alert(this_id);
            if ($(this).attr('src') == '../../images/unchecked.png') {
                $(this).attr('src', '../../images/checkedBlue.png');
                $("#hideCompleted").val('true');
                indicator = 1;
            }
            else {
                $(this).attr('src', '../../images/unchecked.png');
                $("#hideCompleted").val('false');
                indicator = 0;
            }

            $("#taskOptions").submit();
        })

        $(".checkcompleted").each(function () {
            if ($(this).attr('value') == "True") {
                //alert("value equals True");
                $(this).next().attr('src', '../../images/checkedBlue.png');
            }
            else {
                //alert("value equals False");
                $(this).next().attr('src', '../../images/unchecked.png');
            }
        })

        $(".this_check_box").click(function () {
            var indicator = 0;
            var this_id = $(this).attr('id');
            //alert(this_id);
            if ($(this).attr('src') == '../../images/unchecked.png') {
                $(this).attr('src', '../../images/checkedBlue.png');
                indicator = 1;
            }
            else {
                $(this).attr('src', '../../images/unchecked.png');
                indicator = 0;
            }
            $.ajax({
                url: '@Url.Action("ToggleTaskComplete", "Home")',
                type: "POST",
                data: { id: this_id.toString(), i: indicator.toString() },
                success: function (data) { /*alert("success");*/ },
                error: function (data) { alert("Error occured: data was not updated correctly!"); }
            });
        });
    });
</script>